#
# $Id: Makefile,v 1.3 2004/04/22 09:41:16 york Exp $
#
# Copyright (c) 2004 by TapTarget. All rights reserved.
#

include ../version.inc

TARGET  = sample
VERSION = $(VERSION_MAJOR).$(VERSION_MINOR)

DEBUG_CFLAGS   = -Wall -g -DPILOT -DDEBUG
RELEASE_CFLAGS = -Wall -O2 -DPILOT

OBJS = sample.o


DEBUG_OBJS      = $(addprefix debug/, $(OBJS))
RELEASE_OBJS    = $(addprefix release/, $(OBJS))

BINS = *.o *.bin *.res $(TARGET)


##############################################################################

CC       = m68k-palmos-gcc
AS       = m68k-palmos-as
OBJRES   = m68k-palmos-obj-res
MULTIGEN = m68k-palmos-multigen
OBJDUMP  = m68k-palmos-objdump
GDB      = m68k-palmos-gdb
BUILDPRC = build-prc
PILRC    = pilrc
SED      = sed

##############################################################################

all: debug release

debug: debug/$(TARGET).prc

release: release/$(TARGET).prc

clean:
	@rm -f $(TARGET).rcp $(TARGET).wmlsc
	@rm -f $(addprefix debug/, $(BINS))
	@rm -f $(addprefix release/, $(BINS))

veryclean: clean
	@rm -f debug/$(TARGET).prc
	@rm -f release/$(TARGET).prc

gdb: debug/$(TARGET).prc
	cmd /c start $(GDB) debug/$(TARGET) --command=$(TARGET).gdbrc

##############################################################################

debug/%.o : %.c
	@echo "Compiling $<..." && \
	$(CC) $(DEBUG_CFLAGS) -o $@ -c $<

release/%.o : %.c
	@echo "Compiling $<..." && \
	$(CC) $(RELEASE_CFLAGS) -o $@ -c $<

##############################################################################
# Resources

$(TARGET).rcp : $(TARGET).rcp.template
	$(SED) -e "s/__version/$(VERSION)/g" $< > $@

$(TARGET).wmlsc : $(TARGET).wmls
	../tools/wmlsc/wmlsc -e $(TARGET).xml -l $<

debug/bin.res : $(TARGET).rcp $(TARGET).wmlsc
	$(PILRC) -q -D DEBUG $(TARGET).rcp debug/
	@touch debug/bin.res

release/bin.res : $(TARGET).rcp $(TARGET).wmlsc
	$(PILRC) -q $(TARGET).rcp release/
	@touch release/bin.res

##############################################################################
# Binary assembly

debug/$(TARGET) : $(DEBUG_OBJS)
	@echo "Linking..." && \
	$(CC) -o $@ $^ -g


release/$(TARGET) : $(RELEASE_OBJS)
	@echo "Linking..." && \
	$(CC) -o $@ $^


##############################################################################
# Building PRC

debug/$(TARGET).prc : debug/$(TARGET) debug/bin.res
	@echo "Building program file $@..." && \
	cd debug/ && \
	$(BUILDPRC) -o $(TARGET).prc -c SMPL -n "$(TARGET)" *.bin $(TARGET)

release/$(TARGET).prc : release/$(TARGET) release/bin.res
	@echo "Building program file $@..." && \
	cd release/ && \
	$(BUILDPRC) -o $(TARGET).prc -c SMPL -n "$(TARGET)" *.bin $(TARGET)
